DAY30:Mean Square Error


Posted by birdbirdmurmur on 2023-08-12

題目連結

https://www.codewars.com/kata/51edd51599a189fe7f000015

解法

function solution(firstArray, secondArray){
  let sum = 0;
  for(let i = 0; i < firstArray.length; i++){
    sum += (firstArray[i] - secondArray[i]) ** 2;
  }
  return sum / firstArray.length;
}

筆記

一開始使用雙層迴圈
後來發現兩個array長度一樣
不需要多一個迴圈
同一個index下的數字差
這邊沒有判斷正負
因為平方後一定是正數
最後回傳前 除上陣列長度


#javascript #Codewars #number







Related Posts

測到一個 bug

測到一個 bug

為什麼需要 React / 思考模式的差異 / state vs props

為什麼需要 React / 思考模式的差異 / state vs props

Secure Apache Using Certbot with Let's Encrypt on Ubuntu 20.04

Secure Apache Using Certbot with Let's Encrypt on Ubuntu 20.04


Comments